package com.example.himalaya.presenters;

import android.view.View;

import com.example.himalaya.interfaces.IRecommendPresenter;
import com.example.himalaya.interfaces.IRecommendViewCallback;
import com.example.himalaya.utils.Constans;
import com.example.himalaya.utils.LogUtils;
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.GussLikeAlbumList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther LY
 * @Date 2021/3/23
 **/
public class RecommendPresenter implements IRecommendPresenter {
    private static final String TAG = "RecommendPresenter";
    private static RecommendPresenter sInstance = null;
    private List<IRecommendViewCallback> mCallbacks = new ArrayList<>();

    private RecommendPresenter() {

    }

    public static synchronized RecommendPresenter getsInstance() {
        if (sInstance == null) {
            sInstance = new RecommendPresenter();
        }
        return sInstance;
    }


    @Override
    public void getRecommendList() {
        //获取推荐内容
        getRecommend();
    }

    /**
     * 获取推荐内容,猜你喜欢
     */
    private void getRecommend() {

        updateLoading();
        //封装参数
        Map<String, String> map = new HashMap<>();
        //参数表示一页数据返回多少条
        map.put(DTransferConstants.LIKE_COUNT, Constans.COUNT_RECOMMAND + "");
        CommonRequest.getGuessLikeAlbum(map, new IDataCallBack<GussLikeAlbumList>() {
            @Override
            public void onSuccess(GussLikeAlbumList gussLikeAlbumList) {
                if (gussLikeAlbumList != null) {
                    LogUtils.d(TAG, "thread name  " + Thread.currentThread().getName());
                    List<Album> albumList = gussLikeAlbumList.getAlbumList();
                    if (albumList != null) {
                        LogUtils.d(TAG, "size-->" + albumList.size());
                    }
                    //有数据 更新UI
//                    upRecommendUI(albumList);
                    handlerRecommendResult(albumList);
                }
            }

            @Override
            public void onError(int i, String s) {
                LogUtils.d(TAG, "error--> " + i);
                LogUtils.d(TAG, "errorMsg-->" + s);
                handlerError();
            }
        });
    }

    private void handlerError() {
        if (mCallbacks != null) {
            for (IRecommendViewCallback callback : mCallbacks) {
                callback.onNetworkError();
            }
        }
    }

    private void handlerRecommendResult(List<Album> albumList) {
        //通知UI更新
//        if(albumList ==null) {return;
//        }
//        if (albumList.size()==0) {
//            for (IRecommendViewCallback callback : mCallbacks) {
//                callback.onEmpty();
//            }
//            return;
//        }
//
//        if (mCallbacks != null) {
//            for (IRecommendViewCallback callback : mCallbacks) {
//                callback.onRecommentListLoaded(albumList);
//            }
//        }
        if(albumList !=null) {
            //测试空界面
//            albumList.clear();
            if (albumList.size()==0) {
                for (IRecommendViewCallback callback : mCallbacks) {
                    callback.onEmpty();
                }
            }else {
                for (IRecommendViewCallback callback : mCallbacks) {
                    callback.onRecommentListLoaded(albumList);
                }
            }
        }
    }

    private  void updateLoading(){
        for (IRecommendViewCallback callback : mCallbacks) {
            callback.onLoading();
        }
    }
    @Override
    public void registerViewCallback(IRecommendViewCallback callback) {
        if (mCallbacks != null && !mCallbacks.contains(callback)) {
            mCallbacks.add(callback);
        }

    }

    @Override
    public void unRegisterViewCallback(IRecommendViewCallback callback) {
        if (mCallbacks != null) {
            mCallbacks.remove(callback);
        }
    }

}
